Play
Show Transcript
Video info
00:00
Play
Seek 10 seconds backwards
Seek 10 seconds forward
00:00 / 11:30
Mute
Click to volume controlUse the arrows to control the volume
Enable Captions
Settings
Picture in picture
Fullscreen
More transcript options
Hide Transcript
Skip transcript
00:01
MARINA: Hello, my name is Marina and I am
00:04
currently a student at BYU-Idaho.
00:06
And today I will be talking about functions.
00:09
A function is a group of statements or
00:12
computer commands that
00:13
together perform a task.
00:15
There are different types of functions,
00:17
including built-in functions.
00:19
We can also build our own,
00:21
but we will focus on
00:22
that in the following weeks.
00:24
So let's start with a simple input function.
00:28
For that, we will have to create
00:30
a variable called age.
00:36
We will see input, put parentheses, and
00:39
inside of quotation marks we will ask our question.
00:43
How old are you?
00:47
Something to keep in mind
00:49
is when we use an input function,
00:52
the response from the user
00:54
is automatically string.
00:56
If we want to work with numbers,
00:58
we would have to convert it,
01:00
use it int or float,
01:02
which are both built-in Python functions.
01:06
For example, if we use int,
01:09
we wrap it around our input function.
01:12
And it would give us
01:15
a whole positive number, an integer.
01:18
If we want a float instead of int,
01:22
we just type in float.
01:24
And that will result with a decimal,
01:28
but is usually used for decimals.
01:31
So, for example, if we work
01:33
with prices or such,
01:35
this would be a helpful tool to use.
01:38
So we can just leave it at float.
01:42
Notice in line 3, so far we have called
01:47
two built-in functions, float and input.
01:51
And now we're going to
01:53
call another function, print.
01:58
Inside the parentheses,
02:01
we will say H, because that's what we
02:04
want to be displayed
02:05
after the user answers the question.
02:09
Let's run the code and see how it works.
02:15
As you can see, a question
02:18
is asked, how old are you?
02:19
And we can type in
02:20
any number, for example, 20.
02:26
The print age
02:30
is shown up under the question,
02:34
and we see that there's
02:36
a dot with a zero after 20.
02:38
And that is because we left
02:40
it at float function,
02:42
which means it converted to a decimal.
02:45
To call the function,
02:46
we should keep in mind three things.
02:49
The name of the function,
02:50
which in this case
02:52
it was print function, float function,
02:56
the parameters that the function takes,
02:59
and what the function does.
03:01
To call a function means to write
03:03
a code that causes the computer to
03:06
execute the code that
03:07
is inside that function.
03:09
It is important to know
03:11
the difference between
03:13
a parameter and an argument.
03:15
A parameter is a piece of data that
03:18
a function needs in
03:19
order to complete its task.
03:21
An argument is the value that
03:23
is passed or a parameter into a function.
03:26
For example, in line five, age is an argument.
03:33
Now, let's talk about
03:36
optional versus name arguments.
03:43
When we write a function, then call it,
03:46
we would write the arguments
03:48
inside the parentheses.
03:49
Some arguments are required,
03:51
and others are optional.
03:54
For example, if we want to
03:55
find out things about a student,
03:58
will get asked for their first name,
04:00
last name, and major.
04:01
So, let's start with that.
04:03
Again, we will create a variable.
04:07
Last name, we will equal it to input,
04:12
and we will ask
04:13
the question in the quotations.
04:22
Notice that I left it at
04:26
just in but without using int or float.
04:29
And that's because we are
04:31
expecting to get a string from the user.
04:34
And int and float is used for numbers.
04:38
So here we create it.
04:40
Last name, and then we're creating the first name.
04:44
[silence]
04:52
And major.
04:54
[silence]
05:05
As you can see, lines three through five,
05:10
we called the input function three times.
05:18
An option argument can include
05:22
something like the separate argument.
05:28
And I will show
05:30
you it in the print function.
05:33
So, if we want to display the first name,
05:38
last name, and the major of a student in the print function,
05:41
we will specify those variable names.
05:47
First name, last name, major.
05:51
Now, after the comma,
05:54
we would rate our separate argument.
06:01
I'm going to put space and then we can
06:04
put all sorts of characters.
06:09
It can be a comma,
06:10
it could be a slash, anything like that.
06:14
And this time I'm just going to use a bar.
06:17
And that leads us into the name arguments,
06:21
which is this highlighted part in line seven,
06:27
which shows that we specifically
06:31
gave a name to the argument, we
06:33
specified what we want it to be.
06:37
So let's run the code again.
06:43
Here,
06:45
it's asking us for the last name.
06:49
We'll say this.
06:50
What is your first name?
06:53
And major can be CIT.
06:59
As you can see, without us doing it manually,
07:06
after each input, there was a bar.
07:10
So, each variable was separated.
07:15
Module is a collection of related functions.
07:20
Python has a standard library
07:22
with different modules.
07:24
For example, math module.
07:26
Before we start calling
07:28
any functions, in order to use a math module,
07:31
we will have to import that.
07:34
And to do that,
07:36
we write the name import, then math.
07:41
Okay?
07:43
In this example, we'll ask
07:45
the user for an area of a square.
07:48
To start,
07:51
we have a new variable called area...equals,
07:56
and in this time we'll use float again,
08:01
because sometimes an area can be a decimal.
08:06
Inside, we will have our input function.
08:12
Again, in quotations,
08:14
we will ask the question,
08:16
"What is the area of the square?"
08:23
And we will be specific and
08:25
say in square inches.
08:34
In line six, we're going to
08:37
calculate the side of that square.
08:41
We will begin by naming a variable side.
08:45
And we will equal it to map.
08:50
Well, we have imported in
08:53
line three, and we put dot, square root.
09:00
And we're going to square root
09:03
whatever number the user is
09:04
going to give us--that area.
09:09
Now, let's have the print function.
09:20
And we will say, "The size of a side of the square is..."
09:35
And we will use the side variable because
09:39
that's the number that we want to
09:40
see as a result.
09:44
And we will say that it will be in inches.
09:49
So, in line five,
09:53
we have two different functions called float and input.
09:59
Then in line six,
10:00
we're using the Math Module to
10:05
square root the input from the user.
10:10
And in line eight,
10:12
we're going to display that calculation.
10:16
So far, we have stored a lot
10:19
of different variables in our code,
10:23
but we are not required
10:25
to do that every time.
10:28
For example, we can use
10:29
the same examples we have here.
10:33
And instead of creating the side variable,
10:37
we can copy this part,
10:47
and insert it in line eight,
10:50
and we can remove remainder of line six.
10:56
Now, let's run the code.
11:01
Here on the bottom, it says,
11:03
"What is the area of the square in square inches?"
11:07
We can say something like 64.
11:11
As a result, we get the size of the side of
11:14
the square is eight inches.
11:17
So everything works correctly.
11:19
Notice again that point zero after the eight,
11:24
because we use
11:26
the float function in line five.
Resume AutoScroll
Copy debug info